package com.chpro.orm.hibernate;

import java.beans.IntrospectionException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.chpro.bean.BeanUtil;
import com.chpro.excel.ExcelUtil;
import com.chpro.pdf.PdfUtil;

public abstract class ServiceBaseImpl<T>  implements
	ServiceBase<T> {
	
	public abstract DaoBase<T> getDao();
	
	@Override
	public boolean save(T obj) {
		return getDao().save(obj);
	}

	@Override
	public boolean update(T obj) {
		return getDao().update(obj);
	}

	@Override
	public boolean delete(Integer id) {
		return getDao().delete(id);
	}

	@Override
	public boolean delete(String id) {
		return getDao().delete(id);
	}

	@Override
	public T findById(Integer id) {
		return getDao().findById(id);
	}

	@Override
	public T findById(String id) {
		return getDao().findById(id);
	}

	@Override
	public boolean deleteByIdList(Integer[] idList) {
		return getDao().deleteByIdList(idList);
	}

	@Override
	public List<T> findAll() {
		return getDao().findAll();
	}

	@Override
	public List<T> findAll(String hql, Object[] value) {
		return getDao().findAll(hql, value);
	}

	@Override
	public List<T> findByPage(int beginIndex, int pageSize) {
		return getDao().findByPage(beginIndex, pageSize);
	}

	@Override
	public List findByPage(String hql, Object[] paras, int beginIndex,
			int pageSize) {
		return getDao().findByPage(hql, paras, beginIndex, pageSize);
	}

	@Override
	public Integer findByPage(String hql, Object[] paras) {
		return getDao().findByPage(hql, paras);
	}

	@Override
	public String findByPageToJSON(int beginIndex, int pageSize) {
		return getDao().findByPageToJSON(beginIndex, pageSize);
	}

	@Override
	public boolean delete(T obj) {
		return getDao().delete(obj);
	}

	@Override
	public void excelExport(OutputStream out) {
		List<T> list = getDao().findByPage(0, 10);
		List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
		for(T t:list){
			try {
				mapList.add(BeanUtil.convertBean(t));
			} catch (IntrospectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		ExcelUtil.getExcelByList(mapList, out);
		return ;
	}

	@Override
	public void pdfExport(OutputStream out)throws Exception {
		List<T> list = getDao().findByPage(0, 10);
		
		List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
		for(T t:list){
			mapList.add(BeanUtil.convertBean(t));
		}
		
		PdfUtil.exportPdf(mapList, out);
	}
	
	
}
